import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import log from '@dfeidao/atom-nodejs/logger/log';
import { systemid } from '../atom/config';

import { IncomingHttpHeaders } from 'http';

interface Message {
	filter: {
		doccode: string;
	};
}


interface IBuffetOrders {
	_id: string;
	doccode: string;
	storage_time: number;
	docdate: number;
	docstatus: string;
	equip_no: string;
	equip_name: string;
	location_no: string;
	location_name: string;
	companyid: string;
	companyname: string;
	commodityid: string;
	commodity_name: string;
	account_sum: number;
	copy_single_way_no: string;

	storage_date?: string;
	doc_date?: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\gitee\dfeidao\daoke-wxapp\src\service\s001,action_id:' + action_id);



	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('buffet_orders', ['_id', 'doccode', 'storage_time', 'docdate', 'docstatus', 'equip_no', 'equip_name', 'location_no', 'location_name', 'companyid', 'companyname', 'commodityid', 'commodity_name', 'account_sum', 'copy_single_way_no'], message.filter, 200, 1, [], []);
	const [data] = await exec<IBuffetOrders>();

	data.forEach((item) => {
		item.storage_date = format(item.storage_time, 'yyyy-mm-dd HH:MM:ss');
		item.doc_date = format(item.docdate, 'yyyy-mm-dd HH:MM:ss');
	});

	log('Service end path:e:\gitee\dfeidao\daoke-wxapp\src\service\s001,action_id:' + action_id);
	return {
		data
	};
}
